package com.step.sprite;

import com.step.Step.Dimension;
import com.step.graphic.Texture;
import com.step.graphic.TextureRegion;
import com.step.math.Vector;
import com.step.mesh.Face;
import com.step.mesh.Mesh;

public class Sprite2D extends Sprite{		
	public Sprite2D(Texture texture){
		super(null, texture);
		position = new Vector(Dimension.d2d);
		rotation = new Vector(0, 0, 0);
		scale = new Vector(1,1);

		Mesh mesh = new Mesh(Dimension.d2d, 4);
		mesh.addVertex(-0.5f, -0.5f, (short)255, (short)255, (short)255, (short)255, 0, 1);
		mesh.addVertex( 0.5f, -0.5f, (short)255, (short)255, (short)255, (short)255, 1, 1);
		mesh.addVertex( 0.5f,  0.5f, (short)255, (short)255, (short)255, (short)255, 1, 0);
		mesh.addVertex(-0.5f,  0.5f, (short)255, (short)255, (short)255, (short)255, 0, 0);
		// 添加索引面，一个矩形由两个三角形面组成
		mesh.addFace(new Face(0, 1, 2));
		mesh.addFace(new Face(0, 2, 3));
		setMesh(mesh);
	}
	
	public Sprite2D(TextureRegion region){
		this(region.getTexture());
		setRegion(region);
	}
	
	@Override
	public boolean is2D() {
		return true;
	}

	@Override
	public boolean is3D() {
		return false;
	}

	public Vector getPosition() {
		return position;
	}

	public void setPosition(Vector position) {
		this.position.set(position.getX(), position.getY(), 0);
	}

	public void setPosition(float x, float y){
		this.position.set(x, y, 0);
	}
	
	public float getRotation() {
		return rotation.getZ();
	}

	public Vector getScale() {
		return scale;
	}
	
	public void setRotation(float angle){
		rotation.setZ(angle);
	}
	
	public void rotate(float amountAngle){
		rotation.add(0, 0, amountAngle);
	}
	
	public void setScale(Vector scale){
		this.scale.set(scale.getX(),scale.getY(),1);
	}
	
	public void setScale(float scaleX, float scaleY){
		this.scale.set(scaleX, scaleY, 1);
	}
	
	public void scale(float scaleAmountX, float scaleAmountY){
		this.scale.add(scaleAmountX, scaleAmountY, 0);
	}
	
	public void scale(Vector scaleAmount){
		this.scale.add(scaleAmount.getX(), scaleAmount.getY(), 0);
	}
}
